package com.jnmd.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jnmd.pojo.Course;
import com.jnmd.pojo.Subject;
import com.jnmd.service.CourseService;
import com.jnmd.service.SubjectService;

//piapiapia
//hiehiehie

//撒地方看看开始苦苦思考思考

@Controller
public class CourseController {

	@Autowired
	private CourseService courseService;
	
	@Autowired
	private SubjectService subjectService;
	
	@RequestMapping("courseList.action")
	public String courseList(Model model){
		
		List<Course> allCourse = courseService.getAllCourse();
		model.addAttribute("allCourse", allCourse);
		
		return "/admin/course/courseManage";
	}
	
	@RequestMapping("addView.action")
	public String addView(Model model){
		List<Subject> subjectList = subjectService.getAllSubject();
		model.addAttribute("subjectList", subjectList);
		return "/admin/course/addCourse";
	}
	
	@RequestMapping("editCourse.action")
	public String findCourseById(int id,Model model){
		
		List<Subject> subjectList = subjectService.getAllSubject();
		model.addAttribute("subjectList", subjectList);
		
		Course course = courseService.findCourseById(id);
		model.addAttribute("course", course);
		
		
		
		return "/admin/course/addCourse";
	}
	
	@RequestMapping("saveOrUpdateCourse.action")
	public String saveOrUpdateCourse(Course course){
	
		if(null==course.getId() || course.getId()==0){
			int i = courseService.addCourse(course);
			System.out.println(i);
			return "redirect:courseList.action";
		}else{
			courseService.updateCourse(course);
			return "redirect:courseList.action";
		}
	}
	
	@RequestMapping("deleteCourse.action")
	@ResponseBody
	public String deleteCourse(int id){
		
		courseService.deleteCourse(id);
		return "success";
	}
	
	
}
